import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/parallelGateway'

const api = {

    /**
     * 新增并行网关
     * @param parallelGateway
     * @returns {Promise<*|undefined>}
     */
    createParallelGateway: (parallelGateway) =>
      axiosClient().post(`${requestUrl}/addInfo`, parallelGateway),

    /**
     * 根据ID删除并行网关
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteParallelGatewayById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新并行网关
     * @param id
     * @param parallelGateway
     * @returns {Promise<*|undefined>}
     */
    updateParallelGatewayFull: (id, parallelGateway) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, parallelGateway),

    /**
     * 根据ID部分更新并行网关
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateParallelGatewayPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询并行网关
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getParallelGatewayById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询并行网关
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getParallelGatewayPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询并行网关
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getParallelGatewayList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.processId) {
            params.append('processId', businessParam.processId)
        }
        if (businessParam.gatewayCode) {
            params.append('gatewayCode', businessParam.gatewayCode)
        }
        if (businessParam.gatewayName) {
            params.append('gatewayName', businessParam.gatewayName)
        }
        if (businessParam.gatewayType) {
            params.append('gatewayType', businessParam.gatewayType)
        }
        if (businessParam.prevNodeId) {
            params.append('prevNodeId', businessParam.prevNodeId)
        }
        if (businessParam.joinCondition) {
            params.append('joinCondition', businessParam.joinCondition)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
